package mvc.helper
{
	import bean.CollectBean;
	import bean.CollectEvent;
	
	import define.DBConst;
	
	import flash.data.SQLConnection;
	
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;
	
	import orm.armory.dao.TblItem_DAO;
	import orm.armory.vo.TblItemVO;
	
	public class DataCollectionHelper
	{
		public function DataCollectionHelper()
		{
			if(_instance != null)
				throw new Error("Singleton!");
			_instance = this;
		}
		// 实例
		private static var _instance:DataCollectionHelper;
		
		public static function getInstance():DataCollectionHelper
		{
			if(_instance == null)
				new DataCollectionHelper();
			return _instance;
		}
		// ↑↑↑ Singleton ↑↑↑
		private var _view:DataCollection;
		
		public function creationCompleteListener(event:FlexEvent):void
		{
			this._view = event.currentTarget as DataCollection;
			
			var armoryConn:SQLConnection = DBConst.armoryConn;
		}
		
		public function btnRunClickListener():void
		{
			TblItem_DAO.getInstance().getTableContent(getTblItemListener);
		}
		private var _tblItemList:ArrayCollection;
		private var _collectTrace:Object;
		
		private function getTblItemListener(TblItemList:ArrayCollection):void
		{
			_tblItemList = TblItemList;
			_collectTrace = {finish: true};
			
			//runCollect(-1);
			runCollect(720);
		}
		private function runCollect(index:int):void
		{
			if(_collectTrace.finish)
			{
				if(_collectTrace.item_id != null)
				{
					var msg:String = index
						+ "\t" + _collectTrace.id
						+ "\t" + _collectTrace.item_id
						+ "\t" + _collectTrace.progress
						+ "\t" + _collectTrace.error
						+ "\t" + _collectTrace.finish;
						
					trace(msg);
					_view.txtLog.text = _view.txtLog.text + msg + "\n";
				}

				index++;
				if(index == _tblItemList.length)
				{
					trace("----- Finish -----");
					return;
				}
				else
				{
					collectData(index);
				}
			}
			
			_view.callLater(runCollect, [index]);
		}
		private function isValidItem(id:int):Boolean
		{
			return false
				|| id == 1142
				|| id == 1153
				|| id == 1214
				|| id == 1259
				|| id == 1281;
		}
		private function collectData(index:int):void
		{
			var itemVo:TblItemVO = _tblItemList.getItemAt(index) as TblItemVO;
			_collectTrace = {finish: false};

			if(itemVo.is_token || !isValidItem(itemVo.id))
			{
				_collectTrace.progress = "is_token";
				_collectTrace.finish = true;
			}
			else
			{
//				if(itemVo.item_id != "45271")
//					continue;
				var collectBean:CollectBean = new CollectBean(itemVo);
				collectBean.addEventListener(CollectEvent.COLLECT_START, startListener);
				collectBean.addEventListener(CollectEvent.COLLECT_ERROR, errorListener);
				collectBean.addEventListener(CollectEvent.COLLECT_PROGRESS, progressListener);
				collectBean.addEventListener(CollectEvent.COLLECT_FINISH, finishListener);
				collectBean.run();
			}
		}
		private function startListener(event:CollectEvent):void
		{
			_collectTrace.id = event.itemVo.id;
			_collectTrace.item_id = event.itemVo.item_id;
			//var msg:String = "\t" + event.itemVo.item_id + "\t" + event.type;
			//trace(msg);
			//_view.txtLog.text = _view.txtLog.text + msg + "\n";
		}
		private function progressListener(event:CollectEvent):void
		{
			_collectTrace.progress = event.message;
			var msg:String = "\t" + event.itemVo.item_id + "\t" + event.type + "\t" + event.message;
			trace(msg);
			//_view.txtLog.text = _view.txtLog.text + msg + "\n";
		}
		private function errorListener(event:CollectEvent):void
		{
			if(_collectTrace.error == null)
				_collectTrace.error = event.message;
			else
				_collectTrace.error = _collectTrace.error + " || " + event.message;
			//var msg:String = event.itemVo.item_id + "\t" + event.type + "\t" + event.message;
			//trace(msg);
			//_view.txtLog.text = _view.txtLog.text + msg + "\n";
		}
		private function finishListener(event:CollectEvent):void
		{
			_collectTrace.finish = true;
			//var msg:String = event.itemVo.item_id + "\t" + event.type;
			//trace(msg);
			//_view.txtLog.text = _view.txtLog.text + msg + "\n";
		}
	}
}